package com.uphone.kingmall.activity;

import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.kingmall.adapter.ShopPhotoAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.ShopPhotoBean;
import com.uphone.kingmall.listener.NetListCallBack;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.intent.IntentData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/18
 *     desc   :
 * </pre>
 */
public class ShopPhotoActivity extends BaseActivity {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private ShopPhotoAdapter shopPhotoAdapter;
    private List<ShopPhotoBean.DataBean> dataList = new ArrayList<>();
    private int page = 0;
    @IntentData
    private int id = -1;


    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_base_list;
    }

    @Override
    public void initView() {
        setStatusBar();
        CommonUtil.initTitle(this, "店铺相册", (LinearLayout) findViewById(R.id.ll));

        recyclerview.setLayoutManager(new GridLayoutManager(this, 4));
        shopPhotoAdapter = new ShopPhotoAdapter(this);
        recyclerview.setAdapter(shopPhotoAdapter);

        initListener();
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                loadData(1);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                loadData(++page);
            }
        });
    }

    @Override
    public void initData() {
        loadData(1);
    }

    private void loadData(int page) {
        this.page = page;
        HttpParams params = new HttpParams();
        params.put("page", page + "");
        params.put("limit", 24);
        params.put("shopId", id);
        CommonUtil.netRv(new ShopPhotoBean(), shopPhotoAdapter, dataList, MyUrl.getShopPhotos, params, page, new NetListCallBack<ShopPhotoBean.DataBean>() {
            @Override
            public void callBack(List<ShopPhotoBean.DataBean> list) {
                dataList = list;
            }
        });
    }
}
